package com.daka.service.impl;

import com.daka.dao.MemberOrgDao;
import com.daka.entity.Member;
import com.daka.entity.MemberOrg;
import com.daka.entity.Organization;
import com.daka.service.MemberOrgService;
import com.daka.service.MemberService;
import com.daka.service.OrganizationService;
import com.daka.service.qnzyz.BHMember;
import com.daka.service.qnzyz.BHMemberService;
import com.daka.service.qnzyz.ConfigUtil;
import com.daka.tools.DKQConstant;
import com.daka.tools.DateUtil;
import com.daka.tools.ServiceRuntimeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 16:33
 */

@Service("memberOrgService")
@Transactional
public class MemberOrgServiceImpl implements MemberOrgService {

    @Autowired
    private MemberService memberService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private MemberOrgDao memberOrgDao;
    @Autowired
    private BHMemberService bhMemberService;


    @Override
    public int insertPU(MemberOrg po) {
        po.setUserType(DKQConstant.ROLES_VOLUNTEER);
        //审核通过
        po.setStatus(DKQConstant.APPLY_STATUS_OK);
        po.setCreatetime(DateUtil.getDate());
        try {
            //提交到北航
            submit2BHVolunteer(po);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //组织成员+1
        Organization organization = organizationService.selectByPrimaryKey(po.getOrgid());
        organization.setMemberCounts(organization.getMemberCounts()+1);
        organizationService.update(organization);

        return memberOrgDao.insert(po);
    }

    @Override
    public int insert(MemberOrg po) {
        po.setCreatetime(DateUtil.getDate());
        return memberOrgDao.insert(po);
    }

    @Override
    public int update(MemberOrg po) {
        return memberOrgDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public MemberOrg selectByPrimaryKey(Integer id) {
        return memberOrgDao.selectByPrimaryKey(id);
    }

    @Override
    public List<MemberOrg> selectByStatement(MemberOrg po) {
        return memberOrgDao.selectByStatement(po);
    }


    /**
     * 将当前用户注册至北航获取志愿者一号通
     * @param
     */
    private void submit2BHVolunteer(MemberOrg memberOrg)throws Exception {
        try {
            //获取北航key
            ConfigUtil configUtil = new ConfigUtil();
            configUtil.buildConfig();

            Member member = memberService.selectByPrimaryKey(memberOrg.getMember());
            Organization organization = organizationService.selectByPrimaryKey(memberOrg.getOrgid());
            if(null == member.getVolunteerCode() || StringUtils.isBlank(member.getVolunteerCode()) || "null".equals(member.getVolunteerCode().toLowerCase())) {
                String voluteerCode = bhMemberService.simpleReg(BHMember.toBHMember(member, organization.getGroupCode()));
                if(StringUtils.isBlank(voluteerCode)) {
                    throw new ServiceRuntimeException("团中央志愿者注册失败");
                }
                member.setVolunteerCode(voluteerCode);

       /////////         PushSystemMesssage.getInstance().agreenVolunteerApply(voluteerCode,groupVolunteer.getVolunteer().getMemberCode());
//                //推送消息至志愿者用户
//                List<String> volunteers = new ArrayList<String>();
//                volunteers.add(groupVolunteer.getVolunteer().getMemberCode());
//                PushMessageUtil.publishSystemMessage("您已经是打卡器的认证用户，一号通为:"+voluteerCode,null,PushMessageUtil.SYSTEM_HELPER ,volunteers);

                memberService.update(member);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
